home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Programming / PPCsiod / SIOD / Streams.scm < prev    next >
Text File  |  1993-09-23  |  2KB  |  61 lines

  1. (macro cons-stream 
  2.        (lambda (x)
  3.                `(cons ,(cadr x)
  4.                       (delay ,(caddr x)))))
  5.  
  6. (define (head x) (car x))
  7.  
  8. (define (tail x) (force (cdr x)))
  9.  
  10. (define the-empty-stream 
  11.         ((named-lambda (empty-stream) 
  12.                        (cons-stream 'empty-stream (empty-stream)))))
  13.  
  14. (define (empty-stream? x) (eq? (head x) 'empty-stream))
  15.  
  16. (define (stream? x)
  17.         (and (pair? x) (delayed-object? (cdr x))))
  18.  
  19. (define (stream->list z)
  20.         (if (empty-stream? z)
  21.             '()
  22.             (cons (head z) (stream->list (tail z)))))
  23.  
  24. (define (list->stream z)
  25.         (if (null? z)
  26.             the-empty-stream
  27.             (cons-stream (car z) (list->stream (cdr z)))))
  28.  
  29. (define (stream-map p s)
  30.         (if (empty-stream? s)
  31.             the-empty-stream
  32.             (cons-stream  (p (head s))
  33.                           (stream-map p (tail s)))))
  34.  
  35. (define (stream-for-each p s)
  36.         (if (empty-stream? s)
  37.             the-empty-stream
  38.             (begin (p (head s))
  39.                    (stream-for-each p (tail s)))))
  40.  
  41. (define (stream-append f s)
  42.         (if (empty-stream? f)
  43.             s
  44.             (cons-stream (head f)
  45.                          (stream-append (tail f) s))))
  46.  
  47. (define (stream-filter p s)
  48.         (cond ((empty-stream? s) the-empty-stream)
  49.               ((p (head s)) (cons-stream (head s)
  50.                                               (stream-filter p (tail s))))
  51.               (else (stream-filter p (tail s)))))
  52.  
  53. (define (stream-ref n s)
  54.         (while (> n 0) 
  55.                (set! n (-1+ n))
  56.                (set! s (tail s)))
  57.         (head s))
  58.  
  59. (define stream-nth stream-ref)
  60.  
  61.